﻿@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Index.cshtml";
}
<style>
    .search .input-group{
        float:left;
    }

    .topPanel .search .input-group
    {
        line-height:unset;
    }

    .search .input-group .form-control
    {
        float:unset;
    }
</style>
<script>

    function showName(cellValue, options, rowObject) {
        if (rowObject.type == 2) {
            return "<a href=\"javascript:void(0);\" onclick=\"btn_details('" + rowObject.id + "')\">" + rowObject.name + "</a>";
        }

        if (rowObject.type == 1) {
            return rowObject.name;
        }
    }

    function showVersion(cellValue, options, rowObject) {
        return rowObject.version;
    }

    function showState(cellValue, options, rowObject) {
        if (rowObject.type == 2) {
            return "<a href=\"javascript:void(0);\" onclick=\"set_state('" + rowObject.id + "'," + (rowObject.deleteMark ? "false" : "true") + ")\"><i class=\"fa fa-toggle-" + (rowObject.deleteMark ? "off" : "on") + "\"></i></a>";
        }

        if (rowObject.type == 1) {
            return (rowObject.deleteMark ? "无效" : "有效");
        }
    }

    function showOperate(cellValue, options, rowObject) {
        if (rowObject.type == 2) {
            if (rowObject.stableVersion != rowObject.version) {
                return "<a href=\"javascript:void(0);\" onclick=\"publish('" + rowObject.id + "')\">发布</a>";
            }
        }
        return "";
    }

    $(function () {
        initControl();
        gridList();
    })

    function initControl() {
        $("#txtCateId").renderSelect({
            url: "/WorkFlow/WFCategory/GetTreeSelectJson",
        });
    }

    function gridList() {
        var $gridList = $("#gridList");
        $gridList.dataGrid({
            treeGrid: true,
            treeGridModel: "adjacency",
            ExpandColumn: "_version",
            url: "/WorkFlow/WFDef/GetTreeGridJson",
            height: $(window).height() - 96,
            beforeRequest: function () {
                $(this).jqGrid("setGridParam", {
                    postData: {
                        cateId: $("#txtCateId").val(),
                        keyword: $("#txt_keyword").val()
                    }
                });
            },
            colModel: [
                { label: "", name: "id", hidden: true, key: true },
                { label: "", name: "version", hidden: true },
                { label: "", name: "type", hidden: true },
                { label: '名称', name: 'name', width: 150, align: 'left', formatter: showName },
                {
                    label: '最新版本', name: '_version', width: 100, align: 'left', formatter: showVersion
                },
                {
                    label: '发布版本', name: 'stableVersion', width: 100, align: 'left'
                },
                {
                    label: '创建时间', name: 'createTime', width: 80, align: 'left',
                    formatter: "date", formatoptions: { srcformat: 'Y-m-d', newformat: 'Y-m-d' }
                },
                {
                    label: "创建人", name: "createUser", width: 80, align: "center"
                },
                {
                    label: "有效", name: "deleteMark", width: 60, align: "center",
                    formatter: showState
                },
                {
                    label: "操作", name: "_operate", width: 100, align: "center",
                    formatter: showOperate
                },
                { label: '备注', name: 'remark', width: 300, align: 'left' }
            ],
            onSelectRow: null
        });

        $("#btn_search").click(function () {
            $("#gridList").trigger('reloadGrid');
        });
    }
    function btn_add() {
        $.modalOpen({
            id: "Form",
            title: "新增流程",
            url: "/WorkFlow/WFDef/Form",
            width: "900px",
            height: "700px",
            callBack: function (iframeId, index) {
                top.frames[iframeId].submitForm(index);
            }
        });
    }

    function btn_edit() {
        if (!$("#gridList").getGridParam("reccount"))
            return;

        var row = $("#gridList").jqGridRowValue();
        if (!row) {
            $.modalAlert("请选择需要的编辑的项", "error");
            return;
        }

        if (row.type == "1") {
            return;
        }

        var editOption = {
            id: "Form",
            title: "编辑流程",
            url: "/WorkFlow/WFDef/Form?keyValue=" + row.id,
            width: "900px",
            height: "700px",
            btn: ["保存", "取消"],
            btnclass: ['btn btn-primary', 'btn btn-danger'],
            callBack: function (iframeId, index) {
                //保存
                top.frames[iframeId].submitForm(index);
            }
        };

        if (row.version == row.stableVersion) {
            $.modalConfirm("发布版本不能编辑，确定要创建新版本吗？", function (result, index) {
                top.layer.close(index);
                if (result) {
                    editOption.callBack = function (iframeId, index) {
                        top.frames[iframeId].submitForm(index, "update");
                    }
                    $.modalOpen(editOption);
                }
                else {
                    btn_details(row.id);
                }
            });
        }
        else {
            $.modalOpen(editOption);
        }
    }

    function btn_delete() {
        if (!$("#gridList").getGridParam("reccount"))
            return;

        //单选
        var row = $("#gridList").jqGridRowValue();
        if (!row) {
            $.modalAlert("请选择需要删除的项", "error");
            return;
        }

        if (row.type == "1") {
            return;
        }

        $.modalConfirm("确定要删除所选项吗？", function (result, index) {
            top.layer.close(index);
            if (result) {
                $.ajax({
                    url: "/WorkFlow/WFDef/Remove",
                    type: "POST",
                    data: { ids: row.id,__RequestVerificationToken: $('[name=__RequestVerificationToken]').val() },
                    success: function (rsp) {
                        if (rsp.message) {
                            $.modalAlert(rsp.message, "error");
                        }
                        $("#gridList").trigger("reloadGrid");
                    }
                });
            }
        });
    }
    function btn_details(id) {

        if (!id)
            return;

        $.modalOpen({
            id: "Details",
            title: "查看",
            url: "/WorkFlow/WFDef/Details?keyValue=" + id,
            width: "900px",
            height: "700px",
            btn: null,
        });
    }

    function set_state(id, s) {

        if (!$("#gridList").getGridParam("reccount"))
            return;

        if (s === undefined) {
            return;
        }

        if (id === null) {
            var row = $("#gridList").jqGridRowValue();
            if (!row) {
                $.modalAlert("请选择需要设置的项！", "error");
                return;
            }

            if (row.type == "1") {
                return;
            }

            id = row.id;
        }

        $.ajax({
            url: "/WorkFlow/WFDef/DeleteForm",
            type: "POST",
            data: { ids: id,state:s, __RequestVerificationToken: $('[name=__RequestVerificationToken]').val() },
            success: function (rsp) {
                if (rsp.message) {
                    $.modalAlert(rsp.message, rsp.state);
                }
                $("#gridList").trigger("reloadGrid");
            }
        });
    }

    function publish(id) {

        if (!$("#gridList").getGridParam("reccount") || !id)
            return;

        if ($.modalConfirm("确定要发布该版本流程吗？", function (result, index) {
            if (result) {
                $.ajax({
                    url: "/WorkFlow/WFDef/Publish",
                    type: "POST",
                    data: { ids: id, __RequestVerificationToken: $('[name=__RequestVerificationToken]').val() },
                    success: function (rsp) {
                        if (rsp.message) {
                            $.modalAlert(rsp.message, rsp.state);
                        }
                        $("#gridList").trigger("reloadGrid");
                    }
                });
            }

            top.layer.close(index);
        }));
    }
</script>

<div class="topPanel" style="height:unset;line-height:40px;padding:5px;display:flex;">
    <div class="search" style="flex:1;">
        <div class="input-group" style="width:230px;">
            <select id="txtCateId" type="text" class="form-control" placeholder="分类">
                <option value="0">全部分类</option>
            </select>
        </div>
        <div class="input-group">
            <input id="txt_keyword" type="text" class="form-control" placeholder="请输入要查询关键字" style="width: 200px;">
            <span class="input-group-btn" style="width:unset;">
                <button id="btn_search" type="button" class="btn btn-primary"><i class="fa fa-search"></i></button>
            </span>
        </div>
    </div>
    <div class="toolbar" style="width:230px;">
        <div class="btn-group">
            <a id="NF-add" class="btn btn-primary dropdown-text" onclick="btn_add()"><i class="fa fa-plus"></i>新建</a>
        </div>
        <div class="btn-group">
            <a id="NF-edit" class="btn btn-primary dropdown-text" onclick="btn_edit()"><i class="fa fa-plus"></i>编辑</a>
        </div>
        <div class="btn-group">
            <a id="NF-delete" class="btn btn-primary dropdown-text" onclick="btn_delete()"><i class="fa fa-plus"></i>删除</a>
        </div>
    </div>
</div>
<div class="gridPanel">
    <table id="gridList"></table>
</div>
